package com.kvest.fragmentsfinaltest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TabHost;
import com.kvest.fragmentsfinaltest.adapter.MainAdapter;
import com.kvest.fragmentsfinaltest.component.CustomViewPager;
import com.kvest.fragmentsfinaltest.fragment.AboutFragment;
import com.kvest.fragmentsfinaltest.fragment.PullToRefreshListFragment;
import com.kvest.fragmentsfinaltest.fragment.SimpleFragment;

public class FragmentsFinalTestActivity extends FragmentActivity implements TabHost.TabContentFactory, TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
    private static final String CURRENT_TAB_EXTRA = "current_tab";

    private View emptyView;
    private MainAdapter adapter;
    private TabHost tabHost;
    private CustomViewPager viewPager;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // инициализация
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
        tabHost.setOnTabChangedListener(this);
        viewPager = (CustomViewPager)findViewById(R.id.fragment_container);
        adapter = new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
        //viewPager.setScrollable(false);

        //createTabs
        createTab("first", SimpleFragment.class, null);
        createTab("second", PullToRefreshListFragment.class, null);
        createTab("third", AboutFragment.class, getBundle("Fragment 3"));
        createTab("fourth", AboutFragment.class, getBundle("Fragment 4"));
        createTab("fifth", AboutFragment.class, getBundle("Last fragment"));

        if (savedInstanceState != null && savedInstanceState.containsKey(CURRENT_TAB_EXTRA)) {
            tabHost.setCurrentTab(savedInstanceState.getInt(CURRENT_TAB_EXTRA));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState)   {
        outState.putInt(CURRENT_TAB_EXTRA, tabHost.getCurrentTab());

        super.onSaveInstanceState(outState);
    }

    private void createTab(String tag, Class<? extends Fragment> _class, Bundle arguments) {
        TabHost.TabSpec spec = tabHost.newTabSpec(tag);
        spec.setIndicator(tag);
        spec.setContent(this);
        tabHost.addTab(spec);

        adapter.addFragment(_class, arguments);
    }

    private Bundle getBundle(String value) {
        Bundle result = new Bundle();
        result.putString(AboutFragment.TEXT_ARGUMENT, value);

        return result;
    }

    @Override
    public View createTabContent(String s) {
        if (emptyView == null) {
            emptyView = new View(this);
        }

        return emptyView;
    }

    @Override
    public void onTabChanged(String s) {
        viewPager.setCurrentItem(tabHost.getCurrentTab(), true);
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onPageSelected(int i) {
        tabHost.setCurrentTab(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
